<?php
namespace app\__MODULE__\model;
use think\Model;


class Question extends Model {
    
   
    /**
     * 数据表主键
     * @var string
     */

    protected $pk = 'id';
    
    
     /**
     * 模型名称
     * @var string
     */
    protected $name = 'question';
    protected $autoWriteTimestamp = true;


   /**
     * 取题
     * @param [int] $uid   用户id
     * @return void
     * @author Sam
     * date 2021-03-09
     */
    public static function get_question($uid){
        $question_ids      = Answer::where('uid',$uid)->column('question_id'); //取出已回答题目id
        $where = [];
      
        if(count($question_ids)){
            $where[] = ['id','not in',$question_ids];
        }
        return  Question::where($where)->orderRaw('rand()')->value('id');  //筛选id
    } 
    
    public function getOptionAttr($value,$data){
        return \app\admin\model\Option::where('question_id',$data['id'])->field('id as option_id,option_name,option_number')->select();
    }
    /**
     * 异步获取当前列表 信息
     * @param $where
     * @return array
     */
    public static function getList($where)
    {
        $data=self::setWhere($where)->page((int)$where['page'],(int)$where['limit'])->select();
        $data=count($data) ? $data->toArray() : [];
        $count=self::setWhere($where)->count();
        return compact('data','count');
    }

    /**
     * 获取查询条件
     * @param $where
     * @param string $alert
     * @param null $model
     * @return SystemUserLevel|null
     */
    public static function setWhere($where,$alert='',$model=null)
    {
        $model=$model===null ? new self() : $model;
        return $model;
    }
}

